package doyouknow.group.CnAndZhb.controller;

import com.alibaba.fastjson.JSONObject;
import doyouknow.group.CnAndZhb.entity.TbSceneGwMap;
import doyouknow.group.CnAndZhb.entity.TblGateway;
import doyouknow.group.CnAndZhb.response.BaseControllerReturn;
import doyouknow.group.CnAndZhb.service.TblGatewayService;
import doyouknow.group.CnAndZhb.utils.SerializedField;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;


/**
 * (TblGateway)表控制层
 *
 * @author makejava
 * @since 2020-11-10 17:37:40
 */
@CrossOrigin
@RestController
@RequestMapping("tblGateway")
public class TblGatewayController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TblGatewayService tblGatewayService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TblGateway selectOne(Integer id) {
        return this.tblGatewayService.queryById(id);
    }


    @GetMapping("selectOneByGatewayMac")
    @SerializedField(excludes = {"reboot_gw"})
    public Object selectOneByGatewayMac(String mac) {
        TblGateway tblGateway = tblGatewayService.queryByMac(mac);
        return BaseControllerReturn.create(tblGateway, null);
    }

    @GetMapping("selectTopic")
    public String selectTopic(String mac) {
        TblGateway tblGateway = this.tblGatewayService.queryByMac(mac);
        return tblGateway.getMqttSubTopic();
    }

    @GetMapping("selectTopic1")
    public String selectTopic1(@RequestBody JSONObject jsonObject) {
        String mac = jsonObject.getString("mac");
        TblGateway tblGateway = this.tblGatewayService.queryByMac(mac);
        return tblGateway.getMqttSubTopic();
    }

    @GetMapping("selectTopic2")
    public Object selectTopic2(@RequestBody JSONObject jsonObject) {
        String mac = jsonObject.getString("mac");
        TblGateway tblGateway = this.tblGatewayService.queryByMac(mac);
        JSONObject resultJson = new JSONObject();
        resultJson.put("mqttServer", tblGateway.getMqttServer());
        resultJson.put("mqttPort", tblGateway.getMqttPort());
        resultJson.put("subTopic", tblGateway.getMqttSubTopic());
        return resultJson;
    }

    @GetMapping("selectTopic3")
    public Object selectTopic3(@RequestBody JSONObject jsonObject) {
        String mac = jsonObject.getString("mac");
        TblGateway tblGateway = this.tblGatewayService.queryByMac(mac);
        JSONObject resultJson = new JSONObject();
        resultJson.put("mqttServer", tblGateway.getMqttServer());
        resultJson.put("mqttPort", tblGateway.getMqttPort());
        resultJson.put("subTopic", tblGateway.getMqttSubTopic());
        return resultJson;
    }

    @GetMapping("update/info")
    @SerializedField(enable = false)
    public Object updateGateway(String mac, String type) {
        TblGateway tblGateway = this.tblGatewayService.queryByMac(mac);
        if (tblGateway != null) {
            if (type.equals("update")) {
                if (tblGateway.getDownloadsoftwaredetailidx() != 8) {
                    tblGateway.setDownloadsoftwaredetailidx(8);
                    tblGateway.setDownloadTag(1);
                    this.tblGatewayService.update(tblGateway);
                    return BaseControllerReturn.create(new HashMap<String, String>() {{
                        put("msg", "升级设置成功");
                    }}, null);


                } else {
                    return BaseControllerReturn.create(new HashMap<String, String>() {{
                        put("msg", "已经设置过了");
                    }}, null);
                }
            } else if (type.equals("heart")) {
                if (tblGateway.getMacHeart() != null) {
                    tblGateway.setMacHeart("97983915006f0d00");
                    this.tblGatewayService.update(tblGateway);
                    return BaseControllerReturn.create(new HashMap<String, String>() {{
                        put("msg", "心跳设置成功");
                    }}, null);


                } else {
                    return BaseControllerReturn.create(new HashMap<String, String>() {{
                        put("msg", "已经设置过了");
                    }}, null);
                }
            } else {
                return BaseControllerReturn.create(new HashMap<String, String>() {{
                    put("msg", "参数错误");
                }}, null);
            }

        }else{
            return BaseControllerReturn.create(new HashMap<String, String>() {{
                put("msg", "网关不存在");
            }}, null);
        }
    }
}